﻿using adb;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using System.Linq;
using Z.EntityFramework.Plus;
using Masuit.Tools.Security;
using System.Collections.Generic;
using PowerStation.RequestModel;
using Microsoft.EntityFrameworkCore;
using adb.excel;
using System.IO;
using Masuit.Tools;
using Microsoft.Net.Http.Headers;
using PowerStation.AppCode;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Configuration;
using WebSocketSharp;
using System.Security.Claims;
using System;
using PowerStation.TableModel;

namespace PowerStation.Controllers
{

    /// <summary>
    /// 通知类型
    /// </summary>
    public enum MsgType
    {
        /// <summary>
        /// 才务
        /// </summary>
        CW,
        /// <summary>
        /// 销售单
        /// </summary>
        XSD,
        /// <summary>
        /// 在线人数
        /// </summary>
        LC
    }
    /// <summary>
    /// 基类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    [ApiController, ServiceFilter(typeof(RankCheck)), ArrayInput("searchItem")]
    [AttrSplit]
    public class Api<T> : ControllerBase
    {
        IHttpContextAccessor myContent;
        private MyDbContext __db = null;
        /// <summary>
        /// 业务数据上下文
        /// </summary>
        protected MyDbContext _db
        {
            get
            {
                if (__db == null)
                {
                    __db = this.myContent.HttpContext.RequestServices.GetService(typeof(MyDbContext)) as MyDbContext;
                }
                return __db;
            }
        }
        private LogDbContext __LogDb = null;
        /// <summary>
        /// 专用来恢复数据的
        /// </summary>
        protected LogDbContext _LogDb
        {
            get
            {
                if (__LogDb == null)
                {
                    __LogDb = this.myContent.HttpContext.RequestServices.GetService(typeof(LogDbContext)) as LogDbContext;
                }
                return __LogDb;
            }
        }
        private DeviceDbContext __deviceDB = null;
        /// <summary>
        /// 传感器数据上下文
        /// </summary>
        protected DeviceDbContext _deviceDB
        {
            get
            {
                if (__deviceDB == null)
                {
                    __deviceDB = this.myContent.HttpContext.RequestServices.GetService(typeof(DeviceDbContext)) as DeviceDbContext;
                }
                return __deviceDB;
            }
        }
        /// <summary>
        /// 日志上下文
        /// </summary>
        protected ILogger<T> _log
        {
            get
            {
                return this.myContent.HttpContext.RequestServices.GetService(typeof(ILogger<T>)) as ILogger<T>;
            }
        }

        /// <summary>
        /// 配置文件读取
        /// </summary>
        protected IConfiguration configuart
        {
            get
            {
                return this.myContent.HttpContext.RequestServices.GetService(typeof(IConfiguration)) as IConfiguration;
            }
        }
        /// <summary>
        /// 基类构建
        /// </summary>
        /// <param name="configuart">配置文件</param>
        /// <param name="log">日专</param>
        /// <param name="db">数据库</param>
        /// <param name="content">内容</param>
        public Api(IHttpContextAccessor content)
        {
            this.myContent = content;
        }
        /// <summary>
        /// 登陆的用户ID
        /// </summary>
        protected int userId
        {
            get
            {
                var uId = this.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier);
                if (uId == null) return 0;
                return uId.Value.ConvertTo<int>();
            }
        }
        /// <summary>
        /// 重构
        /// </summary>
        public new HttpContext HttpContext { get { return this.myContent.HttpContext; } }


        /// <summary>
        /// 检查自己是否有权限操作电站数据
        /// </summary>
        /// <param name="powerStationId">电站ID</param>
        /// <exception cref="Exception">出错</exception>
        protected async Task IsCheckPowerStation(decimal powerStationId)
        {
            var m = await this._db.tUserPowerStation.FirstOrDefaultAsync(x => x.ups_user_userId_fk == this.userId && x.ups_powerStation_powerStationId_fk == powerStationId);
            if (m == null)
            {
                throw new Exception("未绑定电站,无法操作电站数据");
            }
        }


    }

    /// <summary>
    /// 正常基类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    [Route("api/[controller]/[action]")]
    public class BaseApi<T> : Api<T>
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="content"></param>
        public BaseApi(IHttpContextAccessor content) : base(content)
        {
        }
    }

    /// <summary>
    /// 正常基类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    //[Route("live/{action}")]
    public class LiveBaseApi<T> : Api<T>
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="content"></param>
        public LiveBaseApi(IHttpContextAccessor content) : base(content)
        {
        }
    }

}
